<template>
  <div class="article">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="mini" type="primary" @click="toAdd">新增</el-button>  
      <el-button size="mini" type="primary" @click="toMap">资讯分类</el-button>
    </div>
    <!-- /按钮 -->


    <!-- 表格 -->
    <el-table size="small" :data="articles.list">
        <el-table-column label="序号" type="index" :index="1"></el-table-column>
        <el-table-column label="新闻标题" prop="title" width="300" ></el-table-column>
        <el-table-column label="分类id" prop="categoryId" ></el-table-column>
        <el-table-column label="作者id" prop="authorId" ></el-table-column>
        <el-table-column label="发布时间" prop="publishTime" width="200" >
            <template slot-scope="scope">{{scope.row.publishTime| fmtDate}}</template>
       </el-table-column>
        <el-table-column label="状态" prop="status"></el-table-column>
        <el-table-column label="阅读次数" prop="readTimes"  align="center"></el-table-column>
        <el-table-column label="点赞数" prop="thumpUp"></el-table-column>
        <el-table-column label="封面图片" prop="cover" width="300" :show-overflow-tooltip="true" align="center"></el-table-column>
        <el-table-column label="操作" width="300px" align="center" fixed="right">
        <template v-slot:default="scope">
          <el-button @click="toEdit(scope.row)"  type="text" size="small"  icon="el-icon-edit" >编辑</el-button>
          <el-button @click="toDel(scope.row)" size="small" type="text" icon="el-icon-delete">删除</el-button>
          <el-button @click="toLook(scope.row)" size="small" type="text">查看文章内容</el-button>
        </template>
      </el-table-column>
    </el-table>
     <!-- /表格 -->
     <!--分页-->
      <el-pagination
          v-if="articles.total > param.pageSize"
          layout="prev, pager, next"
          @current-change="currentChangeHandler"
          :current-page="param.page"
          :page-size="param.pageSize"
          :total="articles.total">
        </el-pagination>
      <!--分页-->
    <!-- 模态框 -->
    <el-dialog title="发布新闻资讯" :visible.sync="visible">
      <el-form label-width="100px" size="small">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
         <el-form-item label="所属分类">
           <el-select v-model="form.categoryId">
               <el-option v-for="c in categorys " :key="c.id" :label="c.name" :value="c.id"> </el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="作者Id">
          <el-input v-model="form.authorId"></el-input>
        </el-form-item>
        <el-form-item label="发布日期：" prop="publishTime">
           <el-date-picker :clearable="true"  v-model="info.stockDate"   type="date"    placeholder="选择日期"  format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" :disabled="disabled"></el-date-picker>
        </el-form-item>
        <el-form-item label="内容">
          <tinymce  v-model="form.content" :height="240" />
        </el-form-item>
        <!--<el-form-item label="封面图片" >
          <el-input v-model="form.photoId"></el-input>
         </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="submit" size="small">确 定</el-button>
        </span>
    </el-dialog>
    <!-- /模态框 -->
    <el-dialog   :visible.sync="visibleLook" width="60%">
        <el-form size="medium" >
          <el-form-item>
          <div v-html="form.title" style="font-size:30px;font-weight:bold;"></div>
          <div style="text-align:right;"><strong>{{form.publishTime | fmtDate}}</strong></div>
          </el-form-item>
            <el-form-item >
                <div v-html="form.content" ></div>
            </el-form-item>
            <el-form-item>
            <!-- <el-collapse v-model="activeNames" @change="handleChange">
                <el-collapse-item title="展开评论" name="1">
                    <div>与现实生活一致：与现实生活的流程、逻辑保持一致，遵循用户习惯的语言和概念；</div>
                    <div>在界面中一致：所有的元素和结构需保持一致，比如：设计样式、图标和文本、元素的位置等。</div>
                </el-collapse-item>
              </el-collapse>-->
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="visibleLook = false" size="small" type="primary">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 模态框 -->
  </div>
</template>

<script>
// 导入ajax工具（本质ajax -> axios -> reqeust）
import {get,post} from '@/utils/request'
import Tinymce from '@/components/Tinymce'
export default {
   components:{Tinymce},
  data(){
    return {
      visible:false,
      visibleLook:false,
       param:{
                page:1,
                pageSize:15
            },
      articles:{},
      categorys:{},
      form:{},
      title:"录入新闻资讯",
      info: {
        stockDate:this.getNowTime(),  //日期
      },
      
    }
  },
  created(){
    // 在vue实例创建完毕后加载分类信息
    this.loadArticleData();
    this.loadCategoryData();
    
  },
 
  methods:{
    //跳转
    toMap(){
      this.$router.push({
        path:"/news/Category",
      })
    },
     //日期
     getNowTime() {
	       var now = new Date();
	       var year = now.getFullYear(); //得到年份
	       var month = now.getMonth(); //得到月份
	       var date = now.getDate(); //得到日期
         var hour =now.getHours();
        
         var mm=now.getMinutes();//默认时分秒 如果传给后台的格式为年月日时分秒，就需要加这个，如若不需要，此行可忽略
	       month = month + 1;
	       month = month.toString().padStart(2, "0");
	       date = date.toString().padStart(2, "0");
	       var defaultDate = `${year}-${month}-${date} ${hour}:${mm}`;
	       console.log(defaultDate)
	       return defaultDate;
	       this.$set(this.info, "stockDate", defaultDate);
	    },
      //查看
      toLook(row){
        this.title = "文章内容";
        // 将row克隆后赋值给form，避免直接修改row影响页面变化
        this.form = Object.assign({},row);
        this.visibleLook = true;
      },
    // 提交表单
    submit(){
      let url = "/article/saveOrUpdate";
      // 修改form，不用直接操作form，先进行克隆（深拷贝）
      let data = Object.assign({},this.form);
      // 提交
      post(url,data).then(response => {
        this.$message({type:"success",message:response.message});
        this.loadArticleData();
        this.visible = false;
      })
    },
    // 去添加
    toAdd(){
      this.title = "录入资讯分类";
      this.form = {};
      this.visible = true;
    },
    // 打开模态框准备修改
    toEdit(row){
      this.title = "修改资讯分类";
      // 将row克隆后赋值给form，避免直接修改row影响页面变化
      this.form = Object.assign({},row);
      this.visible = true;
    },
    toDel(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/article/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadArticleData();
        })
      })
    },
    // 加载资讯分类
    loadArticleData(){
        let url="/article/pageQuery";
        get(url,this.param).then(response =>{
            this.articles=response.data;
        });
     },
    loadCategoryData(){
      let url = "/category/findAll";
      get(url).then(response => {
        this.categorys = response.data;
      });
    },
    //加载评论

    // 监听翻页
    currentChangeHandler(page){
            this.param.page=page;
            this.loadArticleData();
        },
    }
}
</script>


<style scoped>
.el-pagination {
    text-align: center; 

}
</style>